package com.vhackandroid.bremen.quizbox;

import java.util.List;
import java.util.Random;

import com.vhackandroid.bremen.quizbox.R.id;
import com.vhackandroid.bremen.quizbox.data.Fragen;
import com.vhackandroid.bremen.quizbox.data.FragenDB;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class QuizActivity extends  Activity {
	private int gameMode;
	private int currentPlayerID = 0;
	private TextView playerNameText;
	private TextView questionNumberText;
	private String player1Name;
	private String player2Name;
	private TextView quizFrageText;
	private Button quizAnwserAButton;
	private Button quizAnwserBButton;
	private Button quizAnwserCButton;
	private Button quizAnwserDButton;
	private ProgressBar progressTimerBar;
	
	
	private List<Fragen> fragenListe;
	private Fragen currentFrage;
	private int frageCounter = 0;
	
	private int progress = 30;
	
	private int player1Points = 0;
	private int player2Points = 0;
	
	private int player1Scored = 0;
	private int player2Scored = 0;
	
	private AlertDialog readyDialog;
	
	private Button finishButton;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		final SharedPreferences namePreferences = getSharedPreferences("names", MODE_PRIVATE);
		player1Name = namePreferences.getString("player1", "Player1");
		player2Name = namePreferences.getString("player2", "Player2");
		
		setContentView(R.layout.quizactivity_layout);
		
		playerNameText = (TextView) findViewById(R.id.quizPlayerNumberText);
		questionNumberText = (TextView) findViewById(R.id.quizQuestionNumberText);
		quizFrageText = (TextView) findViewById(R.id.quizFrageText);
		quizAnwserAButton = (Button) findViewById(R.id.quizAnwserAButton);
		quizAnwserBButton = (Button) findViewById(R.id.quizAnwserBButton);
		quizAnwserCButton = (Button) findViewById(R.id.quizAnwserCButton);
		quizAnwserDButton = (Button) findViewById(R.id.quizAnwserDButton);
		progressTimerBar = (ProgressBar) findViewById(R.id.progressTimerBar);
		
		finishButton = (Button) findViewById(R.id.finishButton);
		
		quizAnwserAButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				quizTimerHandler.removeCallbacks(quizTimerRunnable);
				progress = 30;
				if(currentPlayerID == 0){
					if(currentFrage.getRichtigeAntwort() == 0){
						player1Points += 1;
						player1Scored = 1;
					}else {
						player1Points -= 1;
						player1Scored = -1;
					}
					currentPlayerID = 1;
					runQuiz();
				}else {
					if(currentFrage.getRichtigeAntwort() == 0){
						player2Points += 1;
						player1Scored = 1;
					}else {
						player2Points -= 1;
						player2Scored = -1;
					}
					currentPlayerID = 0;
					showResult();
				}
				
			}
		});
		quizAnwserBButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				quizTimerHandler.removeCallbacks(quizTimerRunnable);
				progress = 30;
				if(currentPlayerID == 0){
					if(currentFrage.getRichtigeAntwort() == 1){
						player1Points += 1;
						player1Scored = 1;
					}else {
						player1Points -= 1;
						player1Scored = -1;
					}
					currentPlayerID = 1;
					runQuiz();
				}else {
					if(currentFrage.getRichtigeAntwort() == 1){
						player2Points += 1;
						player1Scored = 1;
					}else {
						player2Points -= 1;
						player2Scored = -1;
					}
					currentPlayerID = 0;
					showResult();
				}
				
			}
		});
		
		quizAnwserCButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				quizTimerHandler.removeCallbacks(quizTimerRunnable);
				progress = 30;
				if(currentPlayerID == 0){
					if(currentFrage.getRichtigeAntwort() == 2){
						player1Points += 1;
						player1Scored = 1;
					}else {
						player1Points -= 1;
						player1Scored = -1;
					}
					currentPlayerID = 1;
					runQuiz();
				}else {
					if(currentFrage.getRichtigeAntwort() == 2){
						player2Points += 1;
						player1Scored = 1;
					}else {
						player2Points -= 1;
						player2Scored = -1;
					}
					currentPlayerID = 0;
					showResult();
				}
			}
		});
		quizAnwserDButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				quizTimerHandler.removeCallbacks(quizTimerRunnable);
				progress = 30;
				if(currentPlayerID == 0){
					if(currentFrage.getRichtigeAntwort() == 3){
						player1Points += 1;
						player1Scored = 1;
					}else {
						player1Points -= 1;
						player1Scored = -1;
					}
					currentPlayerID = 1;
					runQuiz();
				}else {
					if(currentFrage.getRichtigeAntwort() == 3){
						player2Points += 1;
						player1Scored = 1;
					}else {
						player2Points -= 1;
						player2Scored = -1;
					}
					currentPlayerID = 0;
					showResult();
				}
			}
		});
		
		
		gameMode = getIntent().getIntExtra("flag", 0);
		
		if(gameMode == 0 ){
			//Single device
			FragenDB db = new FragenDB(this);
			db.open();
	
			fragenListe = db.getAllFragen();
			db.close();
			
			runQuiz();
		}
		
		finishButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				quizTimerHandler.removeCallbacks(quizTimerRunnable);
				Intent intent = new Intent (QuizActivity.this, WinActivity.class);
				if (player1Points > player2Points)
					intent.putExtra("name", player1Name);
				else if(player2Points > player1Points)
					intent.putExtra("name", player2Name);
				else 
					intent.putExtra("name", "-1");
				startActivity(intent);
				QuizActivity.this.finish();
			}
		});
	}
	
	private Runnable quizTimerRunnable = new Runnable() {
		public void run() {
				quizTimerHandler.sendEmptyMessage(0);

			
		}
	};
	
	private Handler quizTimerHandler = new Handler () {
		public void handleMessage (Message msg){
			progress -= 1;
			progressTimerBar.setProgress(progress);
			if(progress < 0) {
				quizAnwserAButton.setEnabled(false);
				quizAnwserBButton.setEnabled(false);
				quizAnwserCButton.setEnabled(false);
				quizAnwserDButton.setEnabled(false);
				progress = 30;
				currentPlayerID = 1;
			}
			else {
				quizTimerHandler.postDelayed(quizTimerRunnable, 1000);
			}
		}
	};
	
	private void runQuiz () {
		if(currentPlayerID == 0){
			playerNameText.setText(player1Name);
			getFrage();
			frageCounter += 1;
			questionNumberText.setText("Frage "+frageCounter);
			quizFrageText.setText(currentFrage.getFrage());
			quizAnwserAButton.setText(currentFrage.getAntwort0());
			quizAnwserBButton.setText(currentFrage.getAntwort1());
			quizAnwserCButton.setText(currentFrage.getAntwort2());
			quizAnwserDButton.setText(currentFrage.getAntwort3());
			
			quizTimerHandler.postDelayed(quizTimerRunnable, 1000);
			
		}else {
			Toast.makeText(QuizActivity.this, player2Name+" ist dran", Toast.LENGTH_LONG).show();
			playerNameText.setText(player2Name);
			
			quizAnwserAButton.setEnabled(true);
			quizAnwserBButton.setEnabled(true);
			quizAnwserCButton.setEnabled(true);
			quizAnwserDButton.setEnabled(true);
			
			progressTimerBar.setProgress(progress);
			
			quizTimerHandler.postDelayed(quizTimerRunnable, 1000);
		}
		
		
	}
	
	private void getFrage(){
		int random = new Random().nextInt(fragenListe.size());
		Fragen frage = fragenListe.get(random);
		if(frage != null){
			currentFrage = frage;
		}else {
			getFrage();
		}
	}
	
	private void showResult (){
		Builder b = new AlertDialog.Builder(QuizActivity.this);
		b.setTitle("");
		String antwort = null; 
		switch(currentFrage.getRichtigeAntwort()) {
		case 0:
			antwort = currentFrage.getAntwort0();
			break;
		case 1:
			antwort = currentFrage.getAntwort1();
			break;
		case 2:
			antwort = currentFrage.getAntwort2();
			break;
		case 3:
			antwort = currentFrage.getAntwort3();
			break;
		}
		
		b.setMessage("Die richtige Antwort war "+antwort+"\n\n"+player1Name+" hat "+player1Points+ " Punkte!"+
				"\n\n"+player2Name+" hat "+player2Points+ " Punkte!");
		b.setPositiveButton("OK", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				readyHandler.removeCallbacks(readyRunnable);
				readyDialog.dismiss();
				runQuiz();
				
			}
		});
		readyDialog = b.create();
		readyDialog.show();
		readyHandler.postDelayed(readyRunnable, 10000);
	}
	
	private Runnable readyRunnable = new Runnable() {
		public void run() {
				readyHandler.sendEmptyMessage(0);
			
		}
	};
	
	private Handler readyHandler = new Handler() {
		public void handleMessage(Message msg){
			readyDialog.dismiss();
			runQuiz();
		}
	};
	
}